package com.lbmb.mdps.workbench.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.MedicineReceive;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.exception.AuditException;
import com.lbmb.mdps.commons.exception.InventoryException;
import com.lbmb.mdps.commons.exception.MedicineException;
import com.lbmb.mdps.commons.exception.PurchaseException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.FileNameUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.workbench.domain.Audit;
import com.lbmb.mdps.workbench.domain.Medicine;
import com.lbmb.mdps.workbench.domain.Purchase;
import com.lbmb.mdps.workbench.service.AuditService;
import com.lbmb.mdps.workbench.service.MedicineService;
import com.lbmb.mdps.workbench.service.PurchaseService;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 23:11
 * description
 */
@CrossOrigin
@RestController
public class MedicineController {

    @Resource
    private MedicineService medicineService;

    @PostMapping( "/workbench/medicine/medicinePageList.do")
    public ReturnObjectVO medicinePageList(@RequestBody MedicineReceive medicineReceive) throws MedicineException {
        /*
         * 分页获取列表
         * @author 林文塨
         * @date 2022/5/30 23:15
         * @param [medicineReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineReceive.setSkipCount((medicineReceive.getPageNo()-1)*medicineReceive.getPageSize());

        List<Medicine> medicineList = medicineService.medicinePageList(medicineReceive);

        rov.setOthers(medicineList);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicine/getMedicineTotal.do")
    public ReturnObjectVO getMedicineTotal(@RequestBody MedicineReceive medicineReceive) throws MedicineException {
        /*
         * 获取分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Integer total = medicineService.getMedicineTotal(medicineReceive);
        rov.setNumber(total);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicine/addMedicine.do")
    public ReturnObjectVO addMedicine(@RequestBody Medicine medicine) throws MedicineException {
        /*
         * 添加
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicine.setCreateTime(DateTimeUtil.getSysTime());
        medicine.setCreateBy(medicine.getCurrentUserId());
        medicine.setDeleteFlag("0");
        medicine.setId(SnowFlakeIdUtil.generateId());

        medicineService.addMedicine(medicine);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicine/editMedicine.do")
    public ReturnObjectVO editMedicine(@RequestBody Medicine medicine) throws MedicineException {
        /*
         * 修改
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicine.setEditTime(DateTimeUtil.getSysTime());
        medicine.setEditBy(medicine.getCurrentUserId());

        medicineService.editMedicine(medicine);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicine/deleteMedicine.do")
    public ReturnObjectVO deleteEmployee(@RequestBody String [] deleteMedicineIds) throws MedicineException {
        /*
         * 删除
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineService.deleteMedicine(deleteMedicineIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping("/workbench/medicine/uploadMedicineImg.do")
    public ReturnObjectVO uploadImg(MultipartFile medicineImg, HttpServletRequest request) throws FileUploadException, IOException{
        /*
         * 上传药品图片
         * @author 林文塨
         * @date 2022/5/31 15:09
         * @param [medicineImg, request]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        String originalFilename = medicineImg.getOriginalFilename();

        if (originalFilename == null) {
            throw new FileUploadException("获得原始上传药品图片名失败！");
        }

        //提取生成文件名UUID+上传图片的后缀.jpg  .png
        String saveFileName = SnowFlakeIdUtil.generateId() + FileNameUtil.getFileType(originalFilename);


        //文件上传的地址
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/img";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        String decodePath = URLDecoder.decode(realPath, "UTF-8");

        //转存
        medicineImg.transferTo(new File(decodePath, saveFileName));

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setMessage(saveFileName);

        return rov;
    }

    @PostMapping( "/workbench/medicine/purchaseMedicine.do")
    public ReturnObjectVO purchaseMedicine(@RequestBody Purchase purchase) throws PurchaseException, AuditException, InventoryException {

        ReturnObjectVO rov = new ReturnObjectVO();

        medicineService.purchaseMedicine(purchase);

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }
}
